<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">应付对账</h1>
        <Table
          :summaryData="summaryData"
          summary
          :tableHeader="tableHeader"
          :tableData="tableData"
          selection
          @selectList="selectTableList"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
      </el-card>
    </div>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import fileRequest from '@/utils/fileRequest'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import service from '@/utils/request'
import { watch } from 'vue'
import { selectList } from '@/plugins/mixin'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PayableReconciliation'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { onMounted, ref } from 'vue'

const tableHeader = ref([
  {
    width: 110,
    change: true,
    label: '客人发票号',
    prop: 'custInvNo'
  },
  {
    change: true,
    width: 90,
    label: '船期',
    prop: 'shipDate'
  },
  {
    change: true,
    type: 'svg',
    label: '船务检核',
    prop: 'exportFlag'
  },
  {
    width: 110,
    change: true,
    label: '报关发票号',
    prop: 'customsInvNo'
  },
  {
    change: true,
    label: '出口类型',
    prop: 'exportType'
  },
  {
    change: true,
    label: '出货日期',
    prop: 'outDate'
  },
  {
    change: true,
    label: '工厂',
    prop: 'vendorName'
  },
  {
    change: true,
    label: '合同号',
    prop: 'factoryContractNo'
  },
  {
    change: true,
    label: '品牌',
    prop: 'custom'
  },
  {
    change: true,
    width: '90',
    label: '工厂指令',
    prop: 'factoryLotNo'
  },
  {
    change: true,
    label: '出货数量',
    prop: 'outPair'
  },
  {
    change: true,
    label: '数量检核',
    prop: 'aoaQty'
  },
  {
    change: true,
    label: '币种',
    prop: 'currency'
  },
  {
    change: true,
    label: '单价',
    prop: 'factoryPrice'
  },
  {
    change: true,
    label: '应付金额',
    prop: 'outAmt'
  },
  {
    change: true,
    label: '对账年月',
    prop: 'aoaDate'
  },
  {
    change: true,
    label: '对账检核',
    prop: 'aoaCfm'
  },
  {
    change: true,
    label: '附件',
    prop: 'outPair'
  },
  {
    change: true,
    label: '备注',
    prop: 'remark'
  },
  {
    change: true,
    label: '建档人员',
    prop: 'sysUser'
  },
  {
    change: true,
    label: '建档日期',
    prop: 'sysDate'
  }
])
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '客人发票号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '付款单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '报关发票号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '工厂',
    selectOptions: []
  },
  {
    dateType: 'month',
    type: 'dateMonth2',
    placeholder: '出货月份',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'dateSingle',
    placeholder: '出货日期开始',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'dateSingle',
    placeholder: '出货日期结束',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '合同号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '工厂指令',
    inputValue: ''
  },
  {
    type: 'select',
    selectLabel: '待付金额',
    selectValue: '全部',
    selectP: '待付金额',
    selectOptions: [
      {
        optionLabel: '全部',
        optionValue: '全部'
      },
      {
        optionLabel: '大于0',
        optionValue: '大于0'
      },
      {
        optionLabel: '等于0',
        optionValue: '等于0'
      }
    ]
  },
  {
    dateType: 'month',
    type: 'dateMonth2',
    placeholder: '对账年月',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '品牌',
    inputValue: ''
  },
  {
    type: 'select',
    selectLabel: '对账状态',
    selectValue: '全部',
    selectP: '对账状态',
    selectOptions: [
      {
        optionLabel: '全部',
        optionValue: '全部'
      },
      {
        optionLabel: '已对账',
        optionValue: '已对账'
      },
      {
        optionLabel: '未对账',
        optionValue: '未对账'
      }
    ]
  },
  {
    type: 'select',
    selectValue: '全部',
    selectP: '对账检核',
    selectLabel: '对账检核',
    selectOptions: [
      {
        optionLabel: '全部',
        optionValue: '全部'
      },
      {
        optionLabel: '未检核',
        optionValue: '未检核'
      },
      {
        optionLabel: '已检核',
        optionValue: '已检核'
      }
    ]
  },
  {
    type: 'select',
    selectValue: '全部',
    selectP: '数量检核',
    selectLabel: '数量检核',
    selectOptions: [
      {
        optionLabel: '全部',
        optionValue: '全部'
      },
      {
        optionLabel: '未检核',
        optionValue: '未检核'
      },
      {
        optionLabel: '已检核',
        optionValue: '已检核'
      }
    ]
  },
  {
    type: 'dateSingle',
    placeholder: '船期开始',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'dateSingle',
    placeholder: '船期结束',
    dateLabel: '',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '全部',
    selectP: '船务检核',
    selectLabel: '船务检核',
    selectOptions: [
      {
        optionLabel: '全部',
        optionValue: '全部'
      },
      {
        optionLabel: '已检核',
        optionValue: '已检核'
      },
      {
        optionLabel: '未检核',
        optionValue: '未检核'
      }
    ]
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any
const formList2 = ref([
  {
    dateType: 'month',
    type: 'dateMonth2',
    placeholder: '对账年月',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '填充',
    bgColor: '#23C6C8'
  },
  {
    type: 'button',
    checkboxLabel: '数量检核',
    bgColor: '#23C6C8'
  },
  {
    type: 'button',
    checkboxLabel: '取消数量检核',
    bgColor: '#1C84C6'
  },
  {
    type: 'button',
    checkboxLabel: '检核',
    bgColor: '#1C84C6'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核',
    bgColor: '#E98B2A'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '待付金额').selectValue =
      '全部'
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账状态').selectValue =
      '全部'
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账检核').selectValue =
      '全部'
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '数量检核').selectValue =
      '全部'
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '船务检核').selectValue =
      '全部'
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/finance/receivable/aoa/download`, {
      custInvNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '客人发票号'
      )?.inputValue,
      payNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '付款单号'
      )?.inputValue,
      customsInvNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '报关发票号'
      )?.inputValue,
      vendorName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工厂')
        ?.selectValue,
      outMonth: formList.value.find((item: any) => item.placeholder === '出货月份')?.dateValue,
      beginTime: formList.value.find((item: any) => item.placeholder === '出货日期开始')?.dateValue,
      endTime: formList.value.find((item: any) => item.placeholder === '出货日期结束')?.dateValue,
      factoryContractNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '合同号'
      )?.inputValue,
      factoryLotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '工厂指令'
      )?.inputValue,
      actAmt:
        formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '待付金额')
          ?.selectValue === '全部'
          ? '2'
          : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '待付金额')
              ?.selectValue === '大于0'
          ? '1'
          : '0',
      aoaDate: formList.value.find((item: any) => item.placeholder === '对账年月')?.dateValue,
      aoaDateStatus:
        formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账状态')
          ?.selectValue === '全部'
          ? '2'
          : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账状态')
              ?.selectValue === '已对账'
          ? '1'
          : '0',
      aoaCfm:
        formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账检核')
          ?.selectValue === '全部'
          ? ''
          : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账检核')
              ?.selectValue === '已检核'
          ? 'Y'
          : 'N',
      aoaQty:
        formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '数量检核')
          ?.selectValue === '全部'
          ? ''
          : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '数量检核')
              ?.selectValue === '已检核'
          ? 'Y'
          : 'N',
      shipDateBegin: formList.value.find((item: any) => item.placeholder === '船期开始')?.dateValue,
      shipDateEnd: formList.value.find((item: any) => item.placeholder === '船期结束')?.dateValue,
      custom: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌'
      )?.inputValue,
      exportFlag:
        formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '船务检核')
          ?.selectValue === '全部'
          ? ''
          : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '船务检核')
              ?.selectValue === '已检核'
          ? 'Y'
          : 'N',
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const findClicked2 = async (content: string) => {
  if (content === '填充') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const res = (await LoginWork.post(`/finance/receivable/aoa/update/aoaDate?aoaDate=${formList2.value[0].dateValue}`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('填充成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '数量检核') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const res = (await LoginWork.post(`/finance/receivable/aoa/update/aoaQtyY`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('数量检核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '取消数量检核') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const res = (await LoginWork.post(`/finance/receivable/aoa/update/aoaQtyN`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('取消数量检核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '检核') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const res = (await LoginWork.post(`/finance/receivable/aoa/update/aoaCfmY`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('检核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '取消检核') {
    if (selectedLists.value.length === 0) {
      JMessage.error('请选择数据')
      return
    }
    const res = (await LoginWork.post(`/finance/receivable/aoa/update/aoaCfmN`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('取消数量检核成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
}
const tableData = ref([])
const imgList = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 11,
    num: 0
  },
  {
    index: 15,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '有欠数')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '无欠数')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 'N'
  } else if (Audited.checked) {
    return 'Y'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/finance/receivable/aoa/payable/list', {
    custInvNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '客人发票号'
    )?.inputValue,
    payNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '付款单号'
    )?.inputValue,
    customsInvNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '报关发票号'
    )?.inputValue,
    vendorName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '工厂')
      ?.selectValue,
    outMonth: formList.value.find((item: any) => item.placeholder === '出货月份')?.dateValue,
    beginTime: formList.value.find((item: any) => item.placeholder === '出货日期开始')?.dateValue,
    endTime: formList.value.find((item: any) => item.placeholder === '出货日期结束')?.dateValue,
    factoryContractNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '合同号'
    )?.inputValue,
    factoryLotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '工厂指令'
    )?.inputValue,
    actAmt:
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '待付金额')
        ?.selectValue === '全部'
        ? '2'
        : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '待付金额')
            ?.selectValue === '大于0'
        ? '1'
        : '0',
    aoaDate: formList.value.find((item: any) => item.placeholder === '对账年月')?.dateValue,
    aoaDateStatus:
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账状态')
        ?.selectValue === '全部'
        ? '2'
        : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账状态')
            ?.selectValue === '已对账'
        ? '1'
        : '0',
    aoaCfm:
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账检核')
        ?.selectValue === '全部'
        ? ''
        : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '对账检核')
            ?.selectValue === '已检核'
        ? 'Y'
        : 'N',
    aoaQty:
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '数量检核')
        ?.selectValue === '全部'
        ? ''
        : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '数量检核')
            ?.selectValue === '已检核'
        ? 'Y'
        : 'N',
    shipDateBegin: formList.value.find((item: any) => item.placeholder === '船期开始')?.dateValue,
    shipDateEnd: formList.value.find((item: any) => item.placeholder === '船期结束')?.dateValue,
    custom: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌'
    )?.inputValue,
    exportFlag:
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '船务检核')
        ?.selectValue === '全部'
        ? ''
        : formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '船务检核')
            ?.selectValue === '已检核'
        ? 'Y'
        : 'N',
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.summaryRoot.outPair // 出货数量
  summaryData.value[1].num = tableDataList.summaryRoot.outAmt // 应付金额
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0]) => {
  currentTableRow.value = row
}
const selectedLists = ref([]) as any
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}

// 获取工厂下拉框
const getSelectList = async () => {
  const res = await LoginWork.post('/subcontract/static/oem/combo')
  formList.value[3].selectOptions = res.data.map((item: any) => {
    return {
      optionKey: item.vendorAbbr,
      // optionValue: `${item.vendorAbbr}  ${item.vendorNo}`,
      optionValue: `${item.vendorAbbr}`,
      optionLabel: item.vendorAbbr
    }
  })
}
onMounted(() => {
  getSelectList()
})
</script>
